package inspur.dao;

import inspur.common.ConnectionUtils;
import inspur.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginDao {

	Connection conn=null;
	PreparedStatement pstm=null;
	ResultSet rs=null;
	public int login(User user){
		
		//得到数据库对象
		conn= ConnectionUtils.getConnection();

		int flag=0;//用户名不存在
		try {
			//发送sql文

			String sql2017="select userid,password from user where userid=?";
			pstm= conn.prepareStatement(sql2017);
			pstm.setString(1, user.getId()
			);
			 rs= pstm.executeQuery();
			 
			 //处理结果集
			 while(rs.next()){
				String temp=rs.getString("password");
				if(temp.equals(user.getPassword())){
					//用户名和密码都正确
					flag=2;
					
				}else{
					//密码不正确
					flag=1;
				}
			 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}finally{
			
			//释放资源
			ConnectionUtils.close(rs, pstm, conn);
		}
		return flag;
		
	}
	
	
}
